package com.carrie.mdg.myviewtext.utils;

import android.app.Activity;
import android.content.Context;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * Created by mdg on 2017/3/23.
 */

public class Utils {

    public static final int convertToInt(Object value, int defaultValue) {
        if (value == null || "".equals(value.toString().trim())) {
            return defaultValue;
        } else {
            try {
                return Integer.valueOf(value.toString());
            } catch (Exception e) {
                try {
                    return Double.valueOf(value.toString()).intValue();
                } catch (Exception ex) {
                    return defaultValue;
                }
            }
        }
    }

    public static final void save(Context context){
        String data = "data to save";
        FileOutputStream out = null;
        BufferedWriter writer = null;
        try {
            out = context.openFileOutput("data",Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(data);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
